﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PInitiatives.Models;
using PInitiatives.ViewModels;
using PInitiatives.Helpers;

namespace PInitiatives.Areas.Administration.Controllers
{ 
    [AccessDeniedAuthorize]
    public class AdminCategoryController : Controller
    {
        private PInitiativesEntities db = new PInitiativesEntities();

        //
        // GET: /Administration/AdminCategory/

        public ViewResult Index()
        {
            var categories = db.Categories.Include(c => c.ParentCategory);
            return View(categories.ToList());
        }

        //
        // GET: /Administration/AdminCategory/Details/5

        public ViewResult Details(int id)
        {
            Category category = db.Categories.Find(id);
            return View(category);
        }

        //
        // GET: /Administration/AdminCategory/Create

        public ActionResult Create()
        {
            
            ViewBag.Parent = new SelectList(CategoryManager.GetCategoryDDL(), "ID", "Name");
            //ViewBag.Parent = new SelectList(db.Categories, "ID", "Name");
            return View();
        } 

        //
        // POST: /Administration/AdminCategory/Create

        [HttpPost]
        public ActionResult Create(CategoryDetailsViewModel item)
        {
            if (ModelState.IsValid)
            {
                CategoryManager.CreateNewCategory(item.Name, item.Parent);
                
                //Category newCategory = new Category { ID = category.ID, Name = category.Name, Parent = category.Parent };
                //db.Categories.Add(category);
                //db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.Parent = new SelectList(CategoryManager.GetCategoryDDL(), "ID", "Name");
            //ViewBag.Parent = new SelectList(db.Categories, "ID", "Name", category.Parent);
            return View(item);
        }
        
        //
        // GET: /Administration/AdminCategory/Edit/5
 
        public ActionResult Edit(int id)
        {
            Category category = db.Categories.Find(id);
            ViewBag.Parent = new SelectList(CategoryManager.GetCategoryDDL(), "ID", "Name", category.Parent);
            CategoryDetailsViewModel item = new CategoryDetailsViewModel(category);
            return View(item);
        }

        //
        // POST: /Administration/AdminCategory/Edit/5

        [HttpPost]
        public ActionResult Edit(CategoryDetailsViewModel item)
        {
            if (ModelState.IsValid)
            {
                //db.Entry(item).State = EntityState.Modified;
                //db.SaveChanges();
                CategoryManager.EditCategory(item.ID, item.Name, item.Parent);
                return RedirectToAction("Index");
            }
            ViewBag.Parent = new SelectList(CategoryManager.GetCategoryDDL(), "ID", "Name", item.Parent);
            return View(item);
        }

        //
        // GET: /Administration/AdminCategory/Delete/5
 
        public ActionResult Delete(int id)
        {
            Category category = db.Categories.Find(id);
            return View(category);
        }

        //
        // POST: /Administration/AdminCategory/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CategoryManager.DeleteCategory(id);
            //Category category = db.Categories.Find(id);
            //db.Categories.Remove(category);
            //db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}